/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package csse3004.GD.DA;

import csse3004.GD.Account;
import csse3004.GD.LoginService;

import java.util.logging.Logger;

import java.sql.*;

/**
 *
 * @author Tom McNair - S41758984
 */
public class LoginDA extends GenericDA implements LoginService {

    private static final Logger m_logger = Logger.getLogger(LoginDA.class.getName());

    @Override
    public final void createAccount(Account newAccount) {


        String sql = "INSERT INTO USER(ID, PASSWORD, TYPE, EMAIL, ENABLED) VALUES(" + newAccount.getUsername() + ", " + newAccount.getPassword() + ", Web, " + newAccount.getEmail() + ", True)";

        if (checkAccountDetails(newAccount.getUsername(), newAccount.getPassword())) {
            try {
                DatabaseQuery(sql);
            } catch (Exception ex) {
                m_logger.info("createAccount() failed\n");
                m_logger.info(ex.toString());
            }
        } else {
            m_logger.info("Account Creation Failed");
        }
    }

    public final int checkNumUsers(String type) {
        String sql = "SELECT Number FROM USER_TOTALS WHERE Type='" + type + "'";

        try {
            ResultSet rs = DatabaseQuery(sql);
            int numAccounts = rs.getInt("Number");
            return numAccounts;
        } catch (Exception ex) {
            m_logger.info("checkNumUsers() failed\n");
            m_logger.info(ex.toString());
        }
        return 0;
    }

    public final boolean checkAccountDetails(String username, String password) {
        if (username == null || password == null) {
            return false;
        }

        long time = new java.util.GregorianCalendar(2012, 5, 22).getTimeInMillis();
        m_logger.info("Time "+time+" \n");
        time = new java.util.GregorianCalendar(2011, 10, 20).getTimeInMillis();
        m_logger.info("Time "+time+" \n");
        time = new java.util.GregorianCalendar(2011, 10, 7).getTimeInMillis();
        m_logger.info("Time "+time+" \n");
        time = new java.util.GregorianCalendar(2011, 9, 2).getTimeInMillis();
        m_logger.info("Time "+time+" \n");
        
        String sql = "SELECT * FROM USERS WHERE USERNAME='" + username + "' AND PASSWORD='" + password + "'";
        m_logger.info(sql);
        try {
            ResultSet rs = DatabaseQuery(sql);

            if (rs.next()) {
                return true;
            } else {
                m_logger.info("Username Doesn't Exist");
                return false;
            }
        } catch (Exception ex) {
            m_logger.info("checkAccountDetails() failed\n");
            m_logger.info(ex.toString());
            return false;
        }
    }

    public final void updateUserTotals(String userType) {
        String sql1 = "UPDATE User_Totals SET Total = (Total + 1) WHERE Type = '" + userType + "'";
        String sql2 = "UPDATE User_Totals SET Total = (Total + 1) WHERE Type = 'Total'";

        try {
            DatabaseQuery(sql1);
            DatabaseQuery(sql2);
        } catch (Exception ex) {
            m_logger.info("updateUserTotals() failed\n");
            m_logger.info(ex.toString());
        }
    }

    public final int getUserID(String username) {
        String sql = "SELECT User_ID FROM Users WHERE Username = '" + username + "'";

        try {
            ResultSet rs = DatabaseQuery(sql);

            if (rs.next()) {
                return rs.getInt("User_ID");
            } else {
                m_logger.info("Username Doesn't Exist");
                return -1;
            }
        } catch (Exception ex) {
            m_logger.info("getUserID() failed\n");
            m_logger.info(ex.toString());
            return -1;
        }
    }

    public final String getUserType(String username) {
        String sql = "SELECT Type FROM Users WHERE Username = '" + username + "'";

        try {
            ResultSet rs = DatabaseQuery(sql);

            if (rs.next()) {
                return rs.getString("Type");
            } else {
                m_logger.info("Username Doesn't Exist");
                return null;
            }
        } catch (Exception ex) {
            m_logger.info("getUserType() failed\n");
            m_logger.info(ex.toString());
            return null;
        }
    }
}
